import * as PIXI from 'pixi.js';
import Image from 'components/image';
import okSrc from 'common/asset/img/award/ok.png';
import injectStore from 'components/injectStore';
import WXConfig from 'common/utils/WXConfig';
import ajax from 'common/utils/ajax';
const { Container } = PIXI;

@injectStore
export default class ShareModal {
  constructor({parent, CONST, store, hasCue}) {

    this.CONST = CONST;
    this.parent = parent;
    this.store = store;
    this.container = new Container();

    if (hasCue) {
      this._renderCue();
    } else {
      this._renderShare();
      this._renderPlayAgain();
    }

  }

  _renderShare = () => {
    const {share: {src, width, height, top}} = this.CONST;
    const {view} = window.CFG;

    if (view.isWx) {
      let shareBtn = new Image({src, width, height, top});
      shareBtn.x = (view.width - width) / 2;
      this.container.addChild(shareBtn);

      shareBtn.interactive = true;
      shareBtn.on('tap', async () => {
        // console.log('微信分享！');
        if (window.CFG.api.share.length) {
        //   let data = await ajax.req('share', {}, 'post');
          WXConfig.ready();
        }
      });
    }

  };

  _renderPlayAgain = () => {
    const {playAgain: {src, width, height, top}} = this.CONST;
    const {view} = window.CFG;

    let playAgainBtn = new Image({src, width, height, top});
    playAgainBtn.x = (view.width - width) / 2;

    this.container.addChild(playAgainBtn);

    playAgainBtn.interactive = true;
    playAgainBtn.on('tap', () => {
      location.reload();
      // console.log(this.store);
      // this.store.app.enterScene('loading');
    });
  };

  _renderCue = () => {
    const {cue: {src, width, height}} = this.CONST;
    const {view} = window.CFG;

    let cue = new Image({src, width, height});
    cue.x = (view.width - width) / 2;
    cue.y = (view.height - height) / 2;
    this.container.addChild(cue);

    let okBtn = new Image({src: okSrc, width: 282, height: 75});
    okBtn.x = (view.width - okBtn.width) / 2;
    okBtn.y = cue.y + 330;
    this.container.addChild(okBtn);

    okBtn.interactive = true;
    okBtn.on('tap', () => {
      this.container.removeChild(cue);
      this.container.removeChild(okBtn);

      this._renderShare();
      this._renderPlayAgain();
    });
  };
}
